#!/bin/bash
:<<EOF


@author  sky_wu
date   2024-07-08

自创的 log for shell  功能脚本；


日志级别 debug-4, info-3, warn-2, error-1
輸出效果:
[ERROR] 2024-07-08 15:15:39 | /home/sky/Desktop/shell/chapter2-variable-变量/log_client_application.sh: line 8 |  this is log message



EOF



#默认日志级别是 info
LOG_LEVEL=3

#$0 当前脚本的文件名
#$1 msg
#$2 outputFilePath
#debug日志
function LOG4S_debug(){
    local  file_path=$2
    content="[DEBUG] $(date '+%Y-%m-%d %H:%M:%S') |  ${BASH_SOURCE[1]}: line ${BASH_LINENO[0]}  | $1   "
    if [ $LOG_LEVEL -ge 4  ]  ; then
        echo -e "\033[47m"  ${content}  "\033[0m"
        if [[ $file_path != ''  ]]; then
             echo -e  ${content}  >> $file_path
        fi

    fi
}
#$0 当前脚本的文件名
#$1 msg
#$2 outputFilePath
#信息日志
function LOG4S_info(){
    local  file_path=$2
    content="[INFO] $(date '+%Y-%m-%d %H:%M:%S')  |  ${BASH_SOURCE[1]}: line ${BASH_LINENO[0]}  | $1  "
    if [ $LOG_LEVEL -ge 3  ] ;  then
        echo -e "\033[102m"  ${content} "\033[0m"
        if [[ $file_path != ''  ]]; then
         echo -e  ${content}  >> $file_path
        fi
    fi
}
#$0 当前脚本的文件名
#$1 msg
#$2 outputFilePath
#警告日志
function LOG4S_warn(){
    local  file_path=$2
    content="[WARN] $(date '+%Y-%m-%d %H:%M:%S') |  ${BASH_SOURCE[1]}: line ${BASH_LINENO[0]}  | $1    "
    if [ $LOG_LEVEL -ge 2  ]  ;  then

      echo -e "\033[103m" ${content} "\033[0m"
      if [[ $file_path != ''  ]]; then
             echo -e  ${content}  >> $file_path
      fi

    fi
}


#$0 当前脚本的文件名
#$1 msg   输出日志消息
#$2 outputFilePath  输出文件
#错误日志
function LOG4S_error(){

    local  file_path=$2
    content="[ERROR] $(date '+%Y-%m-%d %H:%M:%S') |  ${BASH_SOURCE[1]}: line ${BASH_LINENO[0]} | $1     "
    if [ $LOG_LEVEL -ge 1  ]  ;  then
        echo -e "\033[101m" ${content} "\033[0m"
        if [[ $file_path != ''  ]]; then
            echo -e  ${content}  >> $file_path
        fi

    fi



    # echo "parent_call: BASH_LINENO is ${BASH_LINENO[0]}"
    # echo "parent_call: FUNCNAME is ${FUNCNAME[0]}"
    # echo "parent_call: BASH_SOURCE is ${BASH_SOURCE[1]}"

    # for((i = 0  ;  i < ${#BASH_SOURCE[@]}; i++))
    # do
    #     echo "BASH_SOURCE item_${i} is  ${BASH_SOURCE[$i]} "
    # done


}




#日誌級別配置
function LOG4S_config(){

    LOG_LEVEL=$1

}
